package com.miyake.loveviewweb.client.viewer.table;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.shared.model.DataModel;

public class DataWidget extends Composite {
    final String ONLYHEADER = "Header";
    final String ALL = "Table";
    final String COLUMN = "Column separated";
    final String COMMA = "CSV";
    final String TAB = "TSV";
    
    private VerticalPanel panel = new VerticalPanel();
    private Button updateButton;
    private ITable table;
    
    public DataWidget(final DataModel dataModel) {
        VerticalPanel mainPanel = new VerticalPanel();
        initWidget(mainPanel);
        panel.setWidth("100%");
        final ListBox listBox = createListBox();
        HorizontalPanel toolBar = new HorizontalPanel();
        toolBar.add(listBox);
        
        mainPanel.setWidth("100%");
        mainPanel.add(toolBar);
        mainPanel.add(panel);
        updateButton = new Button("Update");
        toolBar.add(updateButton);
        updateButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                updateData(dataModel);
            }
        });
        
        listBox.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                
                updateTable(dataModel, listBox);
            }
        });
        
        updateTable(dataModel, listBox);
    }

    protected void updateData(DataModel dataModel) {
        dataModel.getXyDataBody().setDataColumn(table.getDataColumnList());
        DataServiceWrapper.getInstance().updateData(dataModel);
    }

    private ListBox createListBox() {
        final ListBox listBox = new ListBox();
        listBox.addItem(ONLYHEADER);
        listBox.addItem(ALL);
        listBox.addItem(COMMA);
        listBox.addItem(TAB);
        return listBox;
    }

    private void updateTable(final DataModel dataModel, final ListBox listBox) {
        String sel = listBox.getItemText(listBox.getSelectedIndex());

        if (sel.equals("CSV") || sel.equals("TSV")) {
            updateButton.setVisible(true);
        }
        else {
            updateButton.setVisible(false);
        }
        
        panel.clear();
        if(sel.equals(ONLYHEADER)) {
            table = TableFactory.getInstance().createHeaderTable(dataModel);
        }
        else if (sel.equals(ALL)) {
            table = TableFactory.getInstance().createDataGrid(dataModel);
        }
        else if (sel.equals(COLUMN)) {
            table = TableFactory.getInstance().createColumnTable(dataModel);
        }
        else if (sel.equals(COMMA)) {
            table = TableFactory.getInstance().createXCVTable(dataModel, ",");
        }
        else if (sel.equals(TAB)) {
            table  = TableFactory.getInstance().createXCVTable(dataModel, "\t");
        }
        
        panel.add(table.getUiWidget());
    }
    
}
